home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12254 < prev    next >
Encoding:
Text File  |  1996-08-05  |  11.1 KB  |  402 lines

  1. Path: wilks.demon.co.uk!Ian
  2. From: Ian M Wilks <Ian@wilks.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need a linked list program
  5. Date: Fri, 29 Mar 1996 23:52:46 +0000
  6. Organization: Ian
  7. Distribution: world
  8. Message-ID: <5N$xDCAOfHXxEwSh@wilks.demon.co.uk>
  9. References: <4jf76k$avb@zeus.tcp.co.uk>
  10. NNTP-Posting-Host: wilks.demon.co.uk
  11. X-NNTP-Posting-Host: wilks.demon.co.uk
  12. MIME-Version: 1.0
  13. X-Newsreader: Turnpike Version 1.10 <xKZkCHGGMISoFYLnpOdd648JTZ>
  14.  
  15. In article <4jf76k$avb@zeus.tcp.co.uk>, David <daveg@tcp.co.uk> writes
  16. >Does anyone out there have a linked list program which moves in both
  17. >directions and can also create, delete and search items in the list?
  18. >
  19. >I'm doing a study into data structures at the moment and this would
  20. >really help.
  21. >
  22.  
  23. Hope this helps.  Unfortunately, its in C++ but should point you in the
  24. right direction:
  25.  
  26. #include <fstream.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <conio.h>
  31. #define YES 1
  32. #define NO 0
  33.  
  34. struct address
  35. {
  36.         char name[40];
  37.         char street[40];
  38.         char town[30];
  39.         char county[30];
  40.         char postcode[10];
  41.         address * next;         //pointer to next entry
  42.         address * previous;     //pointer to previous entry
  43. };
  44.  
  45. address *start, *last;
  46. fstream list_file;
  47.  
  48. address * find(char *);
  49. void enter(), search(), save(), load(), list();
  50. void delete_entry(address **, address **);
  51. void store(address *, address **, address **);
  52. void display(address *);
  53. int select_menu();
  54.  
  55. void main()
  56. {
  57.         int another_go = YES;
  58.         start = last = NULL;    //initialize first and last pointers
  59.  
  60.         while(another_go == YES)
  61.         {
  62.                 switch(select_menu())
  63.                 {
  64.                         case 1:
  65.                                 enter();        //enter data into list
  66.                                 break;
  67.                         case 2:
  68.                                 delete_entry(&start, &last);
  69.                                 break;
  70.                         case 3:
  71.                                 list();         //list the data
  72.                                 break;
  73.                         case 4:
  74.                                 search();       //find a record
  75.                                 break;
  76.                         case 5:
  77.                                 save();         //save list to disc file
  78.                                 break;
  79.                         case 6:
  80.                                 load();      //load list from disc file
  81.                                 break;
  82.                         case 7:
  83.                                 another_go = NO;
  84.                                 break;
  85.                         default:
  86.                                 cout << "\n\nInternal Error\n\n";
  87.                 }               //end of switch statement
  88.         }                       //end of while loop
  89. }                               //end of main()
  90.  
  91. int select_menu()
  92. {
  93.         int item;
  94.         clrscr();
  95.         cout << endl << "\t 1\tMake Entry\n" << "\t 2\tDelete Entry\n"
  96.                  << "\t 3\tList Entries\n" << "\t 4\tSearch\n"
  97.                  << "\t 5\tSave to File\n" << "\t 6\tLoad from File\n"
  98.                  << "\t 7\tExit from Program\n\n";
  99.         do
  100.         {
  101.                 cout << "\t\t\tEnter Choice:  ";
  102.                 cin >> item;
  103.         }while(item < 0 || item > 7);
  104.         return(item);
  105. }
  106.  
  107. //Enter data into list
  108. void enter()
  109. {
  110.         address * info;
  111.         char again = 'Y';
  112.         do
  113.         {
  114.                 if(!(info = new address))
  115.                 {//check if memory allocated - if not terminate program
  116.                         cout << "\n\nMEMORY ALLOCATION ERROR";
  117.                         exit(-1);
  118.                 }
  119.                 clrscr();
  120.                 cout << "\n\tEnter Name:  ";
  121.                 gets(info->name);
  122.                 cout << "\n\tEnter Street:  ";
  123.                 gets(info->street);
  124.                 cout << "\n\tEnter Town:  ";
  125.                 gets(info->town);
  126.                 cout << "\n\tEnter County:  ";
  127.                 gets(info->county);
  128.                 cout << "\n\tEnter Postcode:  ";
  129.                 gets(info->postcode);
  130.  
  131.                 store(info, &start, &last);     //put data into list
  132.  
  133.                 cout << "\n\n\nEnter another?  (Y/N)   ";
  134.                 cin >> again;
  135.         }while( again == 'Y' || again == 'y');
  136. }
  137.  
  138. //create linked list
  139. void store(address * i, address ** start, address **last)
  140. {
  141.         if (*start == NULL)     //first record has to be treated
  142.         {                       //specially
  143.                 i->next = NULL;         //if first record - no next 
  144.                                         //record
  145.                 i->previous = NULL;     //if first record - no previous 
  146.                                         //record
  147.                 *last = i;              //store details in last
  148.                 *start = i;             //store details in start
  149.         }
  150.         else                            //not first record
  151.         {
  152.                 (*last)->next = i;
  153.                 i->next = NULL;
  154.                 i->previous = *last;
  155.                 *last = i;
  156.         }
  157. }
  158.  
  159. //remove entry from list
  160. void delete_entry(address ** start, address ** last)
  161. {
  162.         address * info;
  163.         char name[40];
  164.  
  165.         clrscr();
  166.         cout << "\n\n\t\tEnter Name:  ";
  167.         gets(name);
  168.         info = find(name);              //find record
  169.         if(info)
  170.         {
  171.                 if(*start == info)      //if start of list is to be 
  172.                                         //deleted
  173.                 {                       //new start of list is needed
  174.                         *start = info->next;
  175.                         if(*start)
  176.                                 (*start)->previous = NULL;
  177.                         else
  178.                                 *last = NULL;
  179.                 }
  180.                 else
  181.                 {
  182.                         info->previous->next = info->next;
  183.                         if(info != *last)
  184.                                 info->next->previous = info->previous;
  185.                         else
  186.                                 *last = info->previous;
  187.                 }
  188.                 delete info;
  189.         }
  190.         else
  191.                 gotoxy(25, 25);
  192.         cout << "Press a key to continue.";
  193.         getch();
  194. }
  195.  
  196. //find address
  197. address * find(char * name)
  198. {
  199.         address * info;
  200.  
  201.         info = start;
  202.  
  203.         while(info)
  204.         {
  205.                 if(!strcmp(name, info->name))
  206.                         return(info);
  207.                 info = info->next;
  208.         }
  209.         cout << "\n\n\tName not found\n";
  210.         return(NULL);
  211. }
  212.  
  213. //display list
  214. void list()
  215. {
  216.         address * info;
  217.         int count = 0;
  218.  
  219.         clrscr();
  220.         info = start;
  221.  
  222.         while(info)
  223.         {
  224.                 count++;
  225.                 display(info);
  226.                 info = info->next;
  227.  
  228.                 if ( (count % 4) == 0)  //to prevent records scrolling 
  229.                                         //off screen
  230.                 {
  231.                         gotoxy(25,25);
  232.                         cout << "Press a key to continue";
  233.                         getch();
  234.                         clrscr();
  235.                 }
  236.         }
  237.         cout << endl << endl;
  238.         gotoxy(25, 25);
  239.         cout << "Press a key to continue.";
  240.         getch();
  241. }
  242.  
  243. //print list to screen
  244. void display(address *info)
  245. {
  246.         cout << endl << "\t" << info->name << endl
  247.              << "\t" << info->street << endl
  248.              << "\t" << info->town << endl
  249.              << "\t" << info->county
  250.              << ", " << info->postcode << endl;
  251. }
  252.  
  253. //look for name in list
  254. void search()
  255. {
  256.         char name[40];
  257.         address * info;
  258.  
  259.         clrscr();
  260.         cout << "\n\nEnter name to find:  ";
  261.         gets(name);
  262.  
  263.         info = find(name);
  264.  
  265.         if (info)
  266.         {
  267.                 display(info);
  268.                 gotoxy(25, 25);
  269.                 cout << "Press a key to continue.";
  270.                 getch();
  271.         }
  272.         else
  273.         {
  274.                 gotoxy(25, 25);
  275.                 cout << "Press a key to continue.";
  276.                 getch();
  277.         }
  278. }
  279.  
  280.  
  281. //save list to disc
  282. void save()
  283. {
  284.         address * info;
  285.  
  286.         rename("list.dat", "list.old");         //create backup file
  287.         list_file.open("list.dat",ios::out);
  288.         if(!list_file)
  289.         {
  290.                 cout << "\n\nCould not open file.";
  291.                 exit(-1);
  292.         }
  293.         cout << "\n\n\t\tSAVING FILE\n";
  294.  
  295.         info = start;
  296.         while(info)
  297.         {
  298.                 list_file << info->name << endl << info->street << endl
  299.                           << info->town << endl << info->county << endl      
  300.                           << info->postcode << endl;
  301.                 info = info->next;
  302.         }
  303.         list_file.close();
  304. }
  305.  
  306. //load from disc
  307. void load()
  308. {
  309.         address * info;
  310.  
  311.         list_file.open("list.dat", ios::in);
  312.         if(!list_file)
  313.         {
  314.                 cout << "\n\nCould not open file.";
  315.                 exit(-1);
  316.         }
  317.  
  318.         while(start)    //delete any lists already in memory
  319.         {
  320.                 info = start->next;
  321.                 delete info;
  322.                 start = info;
  323.         }
  324.         start = last = NULL;
  325.  
  326.         cout << "\n\n\t\tLOADING FILE\n";
  327.  
  328.         while(!list_file.eof())
  329.         {
  330.                 info = new address;
  331.                 if(!info)
  332.                 {
  333.                         cout << "\n\n\t\tMEMORY ALLOCATION ERROR\n";
  334.                         exit(-1);
  335.                 }
  336.                 list_file.getline(info->name, 40);
  337.                 list_file.getline(info->street, 40);
  338.                 list_file.getline(info->town, 30);
  339.                 list_file.getline(info->county, 30);
  340.                 list_file.getline(info->postcode, 10);
  341.  
  342.                 if(!( list_file.eof()))
  343.                 {
  344.                         store(info, &start, &last);
  345.                 }
  346.         }
  347.         list_file.close();
  348. }
  349.  
  350.  
  351.  
  352. // If you want to store the list in ascending order use this function 
  353. //instead:
  354. /*
  355. void store(address * i, address ** start, address ** last)
  356. {
  357.         address * old,  * point;
  358.  
  359.         if(*last == NULL)               //first element in list
  360.         {
  361.                 i->next = NULL;
  362.                 i->previous = NULL;
  363.                 *last = i;
  364.                 *start = i;
  365.                 return;
  366.         }
  367.  
  368.         point = *start;                 //start at top of list
  369.  
  370.         old = NULL;
  371.  
  372.         while(point)
  373.         {
  374.                 if(strcmp(point->name, i->name) < 0)
  375.                 {
  376.                         old = point;
  377.                         point = point->next;
  378.                 }
  379.                 else
  380.                 {
  381.                         if(point->previous)
  382.                         {
  383.                                 point->previous->next = i;
  384.                                 i->next = point;
  385.                                 i->previous = point->previous;
  386.                                 point->previous = i;
  387.                                 return;
  388.                         }
  389.                         i->next = point;
  390.                         i->previous = NULL;
  391.                         point->previous = i;
  392.                         *start = i;
  393.                         return;
  394.                 }
  395.         }
  396.         old->next = i;
  397.         i->next = NULL;
  398.         i->previous = old;
  399.         *last = i;
  400. }
  401. */
  402.